package cn.itsource.org.controller;

import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.Result;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 介绍controller是干嘛的 就是哪块业务的controller
@Api(value = "部门的API",description="部门相关的功能")
@RequestMapping("/dept")
@RestController // Controller + responseBody 当前的controller返回的都是json
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * restful风格 localhost:8080/dept/2
     * 这种方式叫做路径变量。我当前的参数是以请求路径的形式传入的
     * 1.路径变量一般只会传一些比较简单的参数
     * 2.你们后面学习了微服务后，feginClient 就是使用这种方式调用的
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department findById(@ApiParam(value="ID属性",required = true)
                                   @PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    /**
     * 查询所有部门数据
     * @return
     */
    @GetMapping
    public List<Department> findAll() {
        return departmentService.findAll();
    }


    /**
     * 添加或者修改部门数据
     * @param department
     * 注意:
     *  axios请求发送的都是json数据 我们参数如果只写 对象的话，是接不到的
     *  @RequestBody 接收axios请求的数据后，将json数据自动转换成我们的对象
     *      注意：@RequestBody 只能定义一个对象
     */
    @PutMapping
    public Result addOrUpdate(@RequestBody Department department){
        try {
            if (null == department.getId()){
                // 添加
                departmentService.add(department);
            }else {
                // 修改
                departmentService.update(department);
            }
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    /**
     *
     * 根据id删除一个
     * @param id
     * @return
     */

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
       return departmentService.queryByPage(query);
    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping
    public Result batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }


    @GetMapping("/tree")
    public List<Department> findDeptTree(){
        return departmentService.findDeptTree();
    }
}
